package cn.ghostman.app.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import cn.ghostman.app.R;
import cn.ghostman.app.base.BasePager;
import cn.ghostman.app.fragment.ReplaceFragment;
import cn.ghostman.app.pager.AudioPager;
import cn.ghostman.app.pager.NetAudioPager;
import cn.ghostman.app.pager.NetVideoPager;
import cn.ghostman.app.pager.VideoPager;
import cn.ghostman.app.util.LogUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主界面
 *
 * @author zpb
 * @since 2017-04-19
 */
public class MainActivity extends FragmentActivity {

    /**
     * FrameLayout-主要内容
     */
    private FrameLayout mFlMain;

    /**
     * RadioGroup-底部标签
     */
    private RadioGroup mRgBottomTab;

    /**
     * Pager 集合
     */
    private Map<Integer, BasePager> basePagerMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.initView();

        this.initData();

        this.initEvent();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        this.mFlMain = (FrameLayout) this.findViewById(R.id.fl_main);
        this.mRgBottomTab = (RadioGroup) this.findViewById(R.id.rg_bottom_tab);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        // 初始化Fragment-pager
        this.basePagerMap = new HashMap<Integer, BasePager>();
        //this.basePagerList.add(new VideoPager(this));
        //this.basePagerList.add(new AudioPager(this));
        //this.basePagerList.add(new NetVideoPager(this));
        //this.basePagerList.add(new NetAudioPager(this));
    }

    /**
     * 初始化事件
     */
    private void initEvent() {
        this.mRgBottomTab.setOnCheckedChangeListener(new CheckedChangeListener());

        // 默认选中“本地视频” XXX 顺序在监听事件之后
        this.mRgBottomTab.check(R.id.rb_video);
    }

    /**
     * RadioGroup 选择事件监听
     */
    private class CheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 获取下标得到集合中相对应的Page
            //int position = group.indexOfChild(findViewById(checkedId));
            BasePager basePager = basePagerMap.get(checkedId);
            if(basePager == null) {
                switch (checkedId) {
                    case R.id.rb_video:
                        basePagerMap.put(checkedId, new VideoPager(getApplicationContext()));
                        break;
                    case R.id.rb_audio:
                        basePagerMap.put(checkedId, new AudioPager(getApplicationContext()));
                        break;
                    case R.id.rb_net_video:
                        basePagerMap.put(checkedId, new NetVideoPager(getApplicationContext()));
                        break;
                    case R.id.rb_net_audio:
                        basePagerMap.put(checkedId, new NetAudioPager(getApplicationContext()));
                        break;
                }
            }

            // 替换Fragment界面
            replaceFragmentPager(basePager);
        }
    }

    /**
     * 替换FragmentPager
     */
    private void replaceFragmentPager(final BasePager basePager) {
        // 1 获取FragmentManager
        FragmentManager manager = getSupportFragmentManager();
        // 2 开启事务
        FragmentTransaction transaction = manager.beginTransaction();
        // 3 替换
        transaction.replace(R.id.fl_main, new ReplaceFragment(basePager));
        // 4 提交事务
        transaction.commit();
    }
}
